/*
 Xern Entity Relocation Network
 Copyright (C) 2010-2011 Ronie Salgado <roniesalg@gmail.com>

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
*/

#ifndef XERN_TRANSFORM_HPP
#define XERN_TRANSFORM_HPP

#include "MathCommon.hpp"
#include "Vector.hpp"
#include "Matrix.hpp"

namespace Xern
{
    class XERN_ALIGNED(16) Transform
    {
    protected:
        Matrix3 object2world_matrix;
        Vector3 object2world_vector;

    public:
        Transform(): object2world_vector(0.0f)
        {
        }

        Transform(const Matrix3 &m, const Vector3 &v)
                : object2world_matrix(m), object2world_vector(v)
        {
        }

        Transform(const Quaternion &q, const Vector3 &v)
                : object2world_matrix(QRotMatrix3(q)), object2world_vector(v)
        {
        }

        Transform(const Transform &t)
                : object2world_matrix(t.object2world_matrix),
                object2world_vector(t.object2world_vector)
        {
        }

        const Transform& operator=(const Transform& t)
        {
            object2world_matrix = t.object2world_matrix;
            object2world_vector = t.object2world_vector;
            return *this;
        }

        const Vector3 &GetVector() const
        {
            return object2world_vector;
        }

        const Matrix3 &GetMatrix() const
        {
            return object2world_matrix;
        }

        Quaternion GetRotation() const
        {
            return object2world_matrix.ToQuaternion();
        }

        void SetRotation(const Quaternion &rot)
        {
        	SetMatrix(QRotMatrix3(rot));
        }

        void SetVector(const Vector3 &v)
        {
            object2world_vector = v;
        }

        virtual void SetMatrix(const Matrix3 &m)
        {
            object2world_matrix = m;
        }

        Matrix4 AsMatrix() const
        {
            const Matrix3 &cm = object2world_matrix;
            const Vector3 &cv = object2world_vector;
            return Matrix4(cm.m1m1, cm.m1m2, cm.m1m3, cv.x,
                           cm.m2m1, cm.m2m2, cm.m2m3, cv.y,
                           cm.m3m1, cm.m3m2, cm.m3m3, cv.z,
                           0.0f,    0.0f,    0.0f,    1.0f);
        }

        Matrix4 AsMatrixTrans() const
        {
            return object2world_matrix * TranslationMatrix(object2world_vector);
        }

        void SetRotation(const Vector3 &v)
        {
            this->SetMatrix(XRotMatrix3(v.x)*YRotMatrix3(v.y)*ZRotMatrix3(v.z));
        }

        Transform Object2World(const Transform &t) const
        {
            // Code extracted from bullet
            return Transform(object2world_matrix*t.object2world_matrix,
                              (*this)*t.object2world_vector);
        }

        Vector3 Object2World(const Vector3 &v) const
        {
            return object2world_matrix*v + object2world_vector;
        }


        friend Vector3 operator*(const Transform &t, const Vector3 &v)
        {
            return t.Object2World(v);
        }

        friend Vector3 operator*(const Vector3 &v, const Transform &t )
        {
            return t.Object2World(v);
        }

        friend Transform operator*(const Transform &t1, const Transform &t2)
        {
            return t1.Object2World(t2);
        }
    };

    class XERN_ALIGNED(16) ReversibleTransform: public Transform
    {
    protected:
        Matrix3 world2object_matrix;

    public:
        ReversibleTransform()
        {
        }

        ReversibleTransform(const Matrix3 &m, const Vector3 &v)
                : Transform(m, v)
        {
            world2object_matrix = object2world_matrix.Inverse();
        }

        ReversibleTransform(const Matrix3 &m, const Matrix3 &im, const Vector3 &v)
                : Transform(m, v), world2object_matrix(im)
        {
        }

        ReversibleTransform(const Quaternion &q, const Vector3 &v)
                : Transform(q, v), world2object_matrix(QRotMatrix3(q.Inverse()))
        {
        }

        ReversibleTransform(const Quaternion &q, const Quaternion &iq,
                             const Vector3 &v)
                : Transform(q, v), world2object_matrix(QRotMatrix3(iq))
        {
        }

        ReversibleTransform(const Transform &t)
                : Transform(t)
        {
            world2object_matrix = t.GetMatrix().Inverse();
        }

        ReversibleTransform(const ReversibleTransform &t)
                : Transform(t), world2object_matrix(t.world2object_matrix)
        {
        }

        const ReversibleTransform& operator=(const ReversibleTransform& t)
        {
            object2world_matrix = t.GetMatrix();
            object2world_vector = t.GetVector();
            world2object_matrix = t.world2object_matrix;
            return *this;
        }

        virtual void SetMatrix(const Matrix3 &m)
        {
            object2world_matrix = m;
            world2object_matrix = m.Inverse();
        }

        const Matrix3 &GetInverseMatrix() const
        {
            return world2object_matrix;
        }

        virtual void SetInverseMatrix(const Matrix3 &m)
        {
            world2object_matrix = m;
            object2world_matrix = m.Inverse();
        }

        ReversibleTransform GetTransformReverseRot()
        {
            return ReversibleTransform(world2object_matrix, object2world_matrix,
                                        object2world_vector);
        }

        Matrix4 AsInverseMatrix() const
        {
            return world2object_matrix * TranslationMatrix(-object2world_vector);
        }

        Matrix4 AsInverseMatrixTrans() const
        {
            return TranslationMatrix(-object2world_vector) * world2object_matrix;
        }

        Vector3 World2Object(const Vector3 &v) const
        {
            return world2object_matrix*(v - object2world_vector);
        }

        ReversibleTransform World2Object(const ReversibleTransform &t) const
        {
            return ReversibleTransform(world2object_matrix * t.object2world_matrix,
                                        t.world2object_matrix*object2world_matrix,
                                        (*this)/t.object2world_vector);
        }

        ReversibleTransform Object2World(const ReversibleTransform &t) const
        {
            // Code extracted from bullet
            return ReversibleTransform(object2world_matrix*t.object2world_matrix,
                                        t.world2object_matrix*world2object_matrix,
                                        (*this)*t.object2world_vector);
        }

        friend ReversibleTransform operator*(const ReversibleTransform &t1,
                                              const ReversibleTransform &t2)
        {
            return t1.Object2World(t2);
        }

        friend ReversibleTransform operator/(const ReversibleTransform &t1,
                                              const ReversibleTransform &t2)
        {
            return t1.World2Object(t2);
        }

        friend Vector3 operator/(const ReversibleTransform &t, const Vector3 &v)
        {
            return t.World2Object(v);
        }

        friend Vector3 operator/(const Vector3 &v, const ReversibleTransform &t )
        {
            return t.World2Object(v);
        }
    };

    class XERN_ALIGNED(16) OrthoTransform: public ReversibleTransform
    {
    public:
        OrthoTransform()
        {
        }

        OrthoTransform(const Matrix3 &m, const Vector3 &v)
                : ReversibleTransform(m, m.Transpose(), v)
        {
        }

        OrthoTransform(const Quaternion &q, const Vector3 &v)
                : ReversibleTransform(q, v)
        {
        }

        OrthoTransform(const Transform &t)
                : ReversibleTransform(t.GetMatrix(),
                                       t.GetMatrix().Transpose(),
                                       t.GetVector())
        {
        }

        virtual void SetMatrix(const Matrix3 &m)
        {
            object2world_matrix = m;
            world2object_matrix = m.Transpose();
        }

        virtual void SetInverseMatrix(const Matrix3 &m)
        {
            world2object_matrix = m;
            object2world_matrix = m.Transpose();
        }

        const OrthoTransform& operator=(const OrthoTransform& t)
        {
            object2world_matrix = t.GetMatrix();
            object2world_vector = t.GetVector();
            world2object_matrix = t.GetInverseMatrix();
            return *this;
        }
    };

};

#endif //XERN_TRANSFORM_HPP

